package com.zz.nondatain.view.activity;

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.text.TextUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

import com.zz.nondatain.base.BaseActivity;
import com.zz.nondatain.databinding.ActivityAddAppealBinding;

/**
 * 添加申诉页面
 */
public class AddAppealActivity extends BaseActivity<ActivityAddAppealBinding> {

    private Calendar selectedDateTime;

    @Override
    protected void initView() {
        // 初始化日期时间选择器
        selectedDateTime = Calendar.getInstance();
    }

    @Override
    protected void initData() {
        // 不需要初始化数据
    }

    @Override
    protected void initObservers() {
        setupListeners();
    }

    @Override
    protected ActivityAddAppealBinding getViewBinding() {
        return ActivityAddAppealBinding.inflate(getLayoutInflater());
    }

    /**
     * 设置监听器
     */
    private void setupListeners() {
        // 返回按钮点击事件
        binding.btnBack.setOnClickListener(v -> finish());

        // 日期选择点击事件
        binding.tvOccurrenceTime.setOnClickListener(v -> showDateTimePicker());

        // 上传图片点击事件
        binding.frameUploadImage.setOnClickListener(v -> {
            // 这里可以实现图片选择功能，为简化起见，仅显示Toast提示
            showToast("选择图片");
        });

        // 提交按钮点击事件
        binding.btnSubmit.setOnClickListener(v -> submitAppeal());
    }

    /**
     * 显示日期时间选择器
     */
    private void showDateTimePicker() {
        // 显示日期选择器
        DatePickerDialog datePickerDialog = new DatePickerDialog(
                this,
                (view, year, month, dayOfMonth) -> {
                    selectedDateTime.set(Calendar.YEAR, year);
                    selectedDateTime.set(Calendar.MONTH, month);
                    selectedDateTime.set(Calendar.DAY_OF_MONTH, dayOfMonth);

                    // 日期选择后显示时间选择器
                    showTimePicker();
                },
                selectedDateTime.get(Calendar.YEAR),
                selectedDateTime.get(Calendar.MONTH),
                selectedDateTime.get(Calendar.DAY_OF_MONTH)
        );
        datePickerDialog.show();
    }

    /**
     * 显示时间选择器
     */
    private void showTimePicker() {
        TimePickerDialog timePickerDialog = new TimePickerDialog(
                this,
                (view, hourOfDay, minute) -> {
                    selectedDateTime.set(Calendar.HOUR_OF_DAY, hourOfDay);
                    selectedDateTime.set(Calendar.MINUTE, minute);
                    selectedDateTime.set(Calendar.SECOND, 0);

                    // 更新UI显示选择的日期时间
                    updateSelectedDateTime();
                },
                selectedDateTime.get(Calendar.HOUR_OF_DAY),
                selectedDateTime.get(Calendar.MINUTE),
                true
        );
        timePickerDialog.show();
    }

    /**
     * 更新所选日期时间显示
     */
    private void updateSelectedDateTime() {
        SimpleDateFormat dateTimeFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss", Locale.getDefault());
        String formattedDateTime = dateTimeFormat.format(selectedDateTime.getTime());
        binding.tvOccurrenceTime.setText(formattedDateTime);
        binding.tvOccurrenceTime.setTextColor(getResources().getColor(android.R.color.black));
    }

    /**
     * 提交申诉
     */
    private void submitAppeal() {
        // 获取输入的申诉信息
        String appealReason = binding.etAppealReason.getText().toString().trim();
        String occurrenceTime = binding.tvOccurrenceTime.getText().toString().trim();
        String factReason = binding.etFactReason.getText().toString().trim();
        String contactPhone = binding.etContactPhone.getText().toString().trim();

        // 验证输入
        if (TextUtils.isEmpty(appealReason)) {
            showToast("请输入申诉事由");
            return;
        }

        if (TextUtils.isEmpty(occurrenceTime) || occurrenceTime.equals("点击选择发生时间 >")) {
            showToast("请选择发生时间");
            return;
        }

        if (TextUtils.isEmpty(factReason)) {
            showToast("请输入事实缘由");
            return;
        }

        if (TextUtils.isEmpty(contactPhone)) {
            showToast("请输入联系电话");
            return;
        }

        // 提交申诉（这里仅显示成功提示，实际应发送到服务器）
        showToast("申诉提交成功");
        
        // 返回上一页
        finish();
    }

} 